package com.grwx.omc.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grwx.omc.admin.entity.SysLookupDetail;
import com.grwx.omc.admin.entity.SysProvider;
import com.grwx.omc.admin.entity.SysRole;
import com.grwx.omc.admin.service.ISysLookupDetailService;
import com.grwx.omc.admin.service.ISysRoleService;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.entity.ResponseData;
import com.grwx.omc.exception.ValidationException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/sys_role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;

    @ApiOperation(value = "查询角色分类")
    @PostMapping("/search/{page}/{size}")
    public ResponseData page(@RequestBody(required = false) SysRole sysRole, @PathVariable("page") int page,
                             @PathVariable("size")  int size) {

        Page<SysRole> sysRolePage = new Page<>(page, size);

        IPage<SysRole> result = iSysRoleService.page(sysRolePage, sysRole);

        return ResponseData.success(result);
    }


    @ApiOperation(value = "新增角色")
    @PostMapping()
    public ResponseData insert(@RequestBody SysRole sysRole) {
        if (null == sysRole) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }
        iSysRoleService.addOrUpdate(sysRole);
        return ResponseData.success(Constant.MsgCode.INSERT_SUCCESS);
    }
    @ApiOperation("角色修改")
    @PutMapping()
    public ResponseData update(@RequestBody @Valid SysRole sysRole){
        if (null == sysRole) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }

        iSysRoleService.addOrUpdate(sysRole);
        return ResponseData.success(Constant.MsgCode.UPDATE_SUCCESS);
    }

    @ApiOperation("角色删除")
    @DeleteMapping()
    public ResponseData delete(Long[] ids){
        if (null==ids || ids.length==0){
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }

        iSysRoleService.del(ids);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }


}
